﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace TaskKernel.DTO
{
    public static class ExtensionsQueue
    {
        public static Queue<string> Unique(this Queue<string> queue)
        {
            var hashSet = new HashSet<string>();
            var uniqueQueue = new Queue<string>();
            int count = queue.Count;
            for (int i = 0; i < count; i++)
                hashSet.Add(queue.Dequeue());
            foreach (string set in hashSet)
                uniqueQueue.Enqueue(set);
            return uniqueQueue;
        }

        public static string ToStringLine(this Queue<string> queue)
        {
            int count = queue.Count;
            string result = "";
            for (int i = 0; i < count; i++)
            {
                result += queue.Dequeue();
                result += "_";
            }
            return result.TrimEnd('_');
        }

        public static string ToStringLine(this Queue<AbstractSortField> queue)
        {
            int count = queue.Count;
            string result = "";
            for (int i = 0; i < count; i++)
            {
                AbstractSortField field = queue.Dequeue();
                result += field.Field + "_" + (field.SortDirectionAsc ? "ASC" : "DESC");
                result += ":";
            }
            return result.TrimEnd(':');
        }

        public static Queue<AbstractSortField> ToQueue(this string inputString)
        {
            if (!String.IsNullOrEmpty(inputString))
            {
                var queue = new Queue<AbstractSortField>();
                foreach (string field in inputString.Split(':'))
                {
                    string[] elem = field.Split('_');
                    if (elem.Count() == 2)
                        queue.Enqueue(new AbstractSortField(elem[0], (elem[1] == "ASC" ? true : false)));
                }
                return queue;
            }
            return new Queue<AbstractSortField>();
        }

        public static Queue<string> StringToQueue(this string inputString)
        {
            if (!String.IsNullOrEmpty(inputString))
            {
                var queue = new Queue<string>();
                foreach (string s in inputString.Split('_'))
                    queue.Enqueue(s);
                return queue;
            }
            return new Queue<string>();
        }

        public static IEnumerable<AbstractTaskList> ThenBy(this IOrderedQueryable<AbstractTaskList> list,
                                                           Queue<AbstractSortField> queueExpression)
        {
            if (queueExpression.Count > 0)
            {
                AbstractSortField field = queueExpression.Dequeue();
                list = field.SortDirectionAsc ? list.ThenBy(field.Field) : list.ThenByDescending(field.Field);
                list = (IOrderedQueryable<AbstractTaskList>) ThenBy(list, queueExpression).AsQueryable();
            }
            return list;
        }
    }
}